package com.hz.zbl.service.impl;

import com.hz.zbl.mapper.ClassMapper;
import com.hz.zbl.mapper.GradeMapper;
import com.hz.zbl.mapper.UserMapper;
import com.hz.zbl.model.dto.StudentDTO;
import com.hz.zbl.model.entity.ClassEntity;
import com.hz.zbl.model.entity.GradeEntity;
import com.hz.zbl.model.entity.UserEntity;
import com.hz.zbl.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    UserMapper userMapper;

    @Resource
    GradeMapper gradeMapper;

    @Resource
    ClassMapper classMapper;

    @Override
    public StudentDTO selectStudentById(int student_id) {
        UserEntity userEntity = userMapper.selectStudentById(student_id);

        if (userEntity == null){
            return null;
        }
        StudentDTO student = new StudentDTO();

        student.setId(userEntity.getId());
        student.setUsername(userEntity.getUsername());
        student.setRealname(userEntity.getRealname());
        student.setStudent_id(userEntity.getStudent_id());
        student.setGrade_id(userEntity.getGrade_id());
        student.setClass_id(userEntity.getClass_id());


        GradeEntity gradeEntity = gradeMapper.selectGradeById(userEntity.getGrade_id());
        student.setGrade_name(gradeEntity.getGrade_name());
        student.setGrade_year(gradeEntity.getGrade_year());


        ClassEntity classEntity = classMapper.selectClassById(userEntity.getClass_id());
        student.setClass_name(classEntity.getClass_name());


        return student;
    }

}
